﻿package org.dm.3d.Actions.object 
{
	import org.dm.3d.geom.Num3D;
	import org.dm.3d.geom.MathExtend;
	import org.dm.3d.Actions.Action;
	import org.dm.3d.object.Bitmap.roundMap;
	
	/**
	 * ...
	 * @author Scott
	 */
	public class Bomb extends Action
	{
		//private var goalPos:Vector.<Vector3D>;
		private var _cur:uint = 0;
		private var t1:uint;
		private var t2:uint;
		private var radius:Number;
		private var radius2:Number;
		private var diff:Array;
		
		public function Bomb() 
		{
			this.isUsed = true;
			t1 = 5;
			t2 = 5;
			radius = 50;
			radius2 = 5000;
			diff = new Array();
		}
		public override function update(items:Array,num:uint):void 
		{
			_cur++;
			if (_cur == 1)
			{
				for (var i:uint = 0; i < num; i++)
				{
					items[i].speed = new Num3D(50*Math.random(), 50*Math.random(), 50*Math.random());
				}				
			}
			if (_cur ==10)
			{
				for (i = 0; i < num; i++)
				{
					items[i].speed = new Num3D();
					var p:Num3D = MathExtend.pInRound(i, 50, 2200); //
					//var p:Num3D =new Num3D(Math.random() * 2 * radius - radius, Math.random() * radius - radius/2, Math.random() * radius/10 - radius/20);
					//var p:Num3D =new Num3D(Math.random()*6000-3000,Math.random()*6000-3000,-Math.random()*1000)
					diff.push(p.subtract(items[i].pos));
					addT1(items[i],diff[i],t1,t2);
				}
			}
			//if (_cur < t1+10) return;
			if (_cur == t1+10)
			{
				for (var j:uint = 0; j < num; j++)
				{
					addT2(items[j],diff[j],t1,t2);
				}
			}
			if (_cur == t1 + t2+8) {
				for (var k:uint = 0; k < num; k++)
				{
					items[k].acceleration = new Num3D();
					items[k].speed = new Num3D();
				}
				diff = new Array();
			}
			/*if (_cur == t1 + t2 + 2)
			{
				for (i = 0; k < num; k++)
				{
					//radius = radius * (.2 + Math.random());
					p = MathExtend.pInRound(i, 50, 600);//new Num3D(Math.random() * 2 * radius2 - radius2, Math.random() * radius2 - radius2/2, Math.random() *  radius2 - radius2/2);
					diff.push(p.subtract(items[k].pos));
					addT1(items[k], diff[k], 5, 3);
				}
			}
			if (_cur == t1 + t2+5)
			{
				for (j = 0; j < num; j++)
				{
					addT2(items[j],diff[j],5,3);
				}
			}
			if (_cur == t1 + t2+8) {
				for (k = 0; k < num; k++)
				{
					items[k].acceleration = new Num3D();
					items[k].speed = new Num3D();
				}
			}*/
			//if(_cur=t1 + t2+2
		}
		/*private function moveTo(cur:uint,pt1:uint,pt2:uint,pr:Number):void 
		{
			if (_cur ==1)
			{
				for (var i:uint = 0; i < num; i++)
				{
					var p:Num3D = new Num3D(Math.random() * 4 * radius - 2*radius, Math.random() * 2 * radius - radius, Math.random() * 2 * radius - radius);
					diff.push(p.subtract(items[i].pos));
					addT1(items[i],diff[i]);
				}
			}
			if (_cur < t1) return;
			if (_cur == t1+1)
			{
				for (var j:uint = 0; j < num; j++)
				{
					addT2(items[j],diff[j]);
				}
			}
			if (_cur == t1 + t2+1) {
				for (var k:uint = 0; k < num; k++)
				{
					items[k].acceleration = new Num3D();
					//items[k].speed = new Num3D();
				}
			}
		}*/
		private function addT1(pitem:roundMap,v:Num3D,pt1:Number,pt2:Number):void 
		{
			var pps:Num3D = v.clone();
			pps.scaleBy(2 / (pt1 * (pt1 + pt2)));
			pitem.damping = 1;
			pitem.acceleration = pps;
			pitem.speed = new Num3D();
		}
		private function addT2(pitem:roundMap,v:Num3D,pt1:Number,pt2:Number):void 
		{
			var pps:Num3D = v.clone();
			pps.scaleBy(-2 / (pt2 * (pt1 + pt2)));
			pitem.acceleration = pps;
		}
	}
	
}
